<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>skalibs: the bitarray library interface</title>
    <meta name="Description" content="skalibs: the bitarray library interface" />
    <meta name="Keywords" content="skalibs c unix bitarray library libstddjb" />
    <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">libstddjb</a><br />
<a href="../libskarnet.html">libskarnet</a><br />
<a href="../index.html">skalibs</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>bitarray</tt> library interface </h1>

<p>
 The following functions are declared in the <tt>skalibs/bitarray.h</tt> header,
and implemented in the <tt>libskarnet.a</tt> or <tt>libskarnet.so</tt> library.
</p>

<h2> General information </h2>

<p>
 <tt>bitarray</tt> is a set of primitives to operate efficiently on
large bitfields.
</p>

<p>
 A bitfield is represented by a pre-allocated block of
<tt>unsigned char</tt>; <tt>bitarray</tt> does not care if that
block has been BSS-, stack- or heap-allocated. Bitfields that
can grow in size should be stored in a
<a href="stralloc.html">stralloc</a>.
</p>

<p>
 Bits in a bitfield of length <em>n</em> are numbered from 0 to <em>n</em>-1.
</p>

<h2> Functions </h2>

<p>
<code> size_t bitarray_div8 (size_t n) </code> <br />
Returns the minimum number of bytes needed to store a field of <em>n</em> bits.

</p>

<p>
<code> void bitarray_clearsetn (unsigned char *s, size_t start, size_t len, int h) </code> <br />
Sets (if <em>h</em> is nonzero) or clears (if <em>h</em> is zero)
<em>len</em> bits in field <em>s</em>, starting at bit <em>start</em>.
</p>

<p>
<code> void bitarray_clearn (unsigned char *s, size_t start, size_t len) </code> <br />
Clears <em>len</em> bits in field <em>s</em>, starting at bit <em>start</em>.
</p>

<p>
<code> void bitarray_setn (unsigned char *s, size_t start, size_t len) </code> <br />
Sets <em>len</em> bits in field <em>s</em>, starting at bit <em>start</em>.
</p>

<p>
<code> int bitarray_peek (unsigned char const *s, size_t n) </code> <br />
Returns the value of the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> void bitarray_poke (unsigned char *s, size_t n, int h) </code> <br />
Sets (if <em>h</em> is nonzero) or clears (if <em>h</em> is zero)
the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> void bitarray_clear (unsigned char *s, size_t n) </code> <br />
Clears the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> void bitarray_set (unsigned char *s, size_t n) </code> <br />
Sets the <em>n</em>th bit in field <em>s</em>.
</p>

<p>
<code> int bitarray_testandpoke (unsigned char *s, size_t n, int h) </code> <br />
Sets (if <em>h</em> is nonzero) or clears (if <em>h</em> is zero)
the <em>n</em>th bit in field <em>s</em>,
and returns the previous value of that bit.
</p>

<p>
<code> int bitarray_testandclear (unsigned char *s, size_t n) </code> <br />
Clear the <em>n</em>th bit in field <em>s</em>,
and returns the previous value of that bit.
</p>

<p>
<code> int bitarray_testandset (unsigned char *s, size_t n) </code> <br />
Sets the <em>n</em>th bit in field <em>s</em>,
and returns the previous value of that bit.
</p>

<p>
<code> size_t bitarray_first (unsigned char const *s, size_t len, int h) </code> <br />
Returns the number of the first set (if <em>h</em> is nonzero) or clear
(if <em>h</em> is zero) bit in <em>s</em>, <em>len</em> being
the total number of bits. If all bits in <em>s</em> are the negation of
<em>h</em>, then <em>len</em> is returned.
</p>

<p>
<code> size_t bitarray_firstclear (unsigned char const *s, size_t len) </code> <br />
Returns the number of the first clear bit in <em>s</em>, <em>len</em> being
the total number of bits. If all bits in <em>s</em> are set, <em>len</em> is returned.
</p>

<p>
<code> size_t bitarray_firstclear_skip (unsigned char const *s, size_t len, size_t skip) </code> <br />
Like <tt>bitarray_firstclear</tt>, but the first <em>skip</em> bits are
ignored: the function cannot return less than <em>skip</em>. It is a
programming error if <em>skip</em> &gt; <em>len</em>.
</p>

<p>
<code> size_t bitarray_firstset (unsigned char const *s, size_t len) </code> <br />
Returns the number of the first set bit in <em>s</em>, <em>len</em> being
the total number of bits. If all bits in <em>s</em> are clear, <em>len</em> is returned.
</p>

<p>
<code> size_t bitarray_firstset_skip (unsigned char const *s, size_t len, size_t skip) </code> <br />
Like <tt>bitarray_firstset</tt>, but the first <em>skip</em> bits are
ignored: the function cannot return less than <em>skip</em>. It is a
programming error if <em>skip</em> &gt; <em>len</em>.
</p>

<p>
<code> size_t bitarray_countones (unsigned char const *s, size_t len) </code> <br />
Returns the number of set bits in <em>s</em>, <em>len</em> being the
total number of bits being tested.
</p>

</body>
</html>
